<cpanel include="../../config.php">

<?

/***************************************************************************
 *                                 postgresql_main.php
 *                            -------------------
 *   Begin                : Monday, April 18, 2005
 *   Copyright            : (C) 2005 Point and Code
 *   Domain               : http://www.pointandcode.com/
 *   Email                : support@pointandcode.com
 *   Version              : 1.0.0
 *
 ***************************************************************************/

/***************************************************************************
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 ***************************************************************************/

$html_root_path = '../../';
include $root_path.'common.php';
$page_title = $lang['postgresql'];
include $root_path.'page_header.php';

//
// Not sure what this is, or why cPanel runs it. Put in here for now, just in case it's something important.
//
$random_thing = <<<EOFpacman
<cpanel Postgres="initcache()">
EOFpacman;



$big_wadge_of_stuff = <<<EOFpacman
<cpanel Postgres="listdbs()">
EOFpacman;

$array_of_databases = explode("<br></blockquote>", $big_wadge_of_stuff);

$array_split_into_users = array();

$i=0;
foreach($array_of_databases as $row)
{
	$temp = explode('<b>', $row);
	$temp2 = explode('</b>', $temp[1]);
	$postgresql_data[$i]['database_name'] = $temp2[0];

	$users_row = explode("<br>", $row);
	
	$postgresql_data[$i]['users'] = array();

	$j = 0;
	foreach ($users_row as $line)
	{
		// The first element contains the database name. We don't want that.
		if ($j != 0)
		{
			// We do want the rest, though.
			$temp = explode(' ', $line);
			$postgresql_data[$i]['users'][$j]['name'] = $temp[0];
		};
		
		$j++;
	};

	$i++;
}

$template->set_filenames(array(
	'body' => $root_path.$template_path.'databases/postgresql_main.tpl')
);

$i = 0;
foreach ($postgresql_data as $row)
{
	$row['database_name'] = trim($row['database_name']);

	if (!empty($row['database_name'])) {
		$template->assign_block_vars('databaserow', array(
			'ROWCLASS' => ($i%2) ? $template_cfg['class_row_1'] : $template_cfg['class_row_2'],
			'NAME' => $row['database_name'],
			'U_DELETE' => 'postgresql_delete.php?db='.$row['database_name'],
			)
		);
		
		foreach($row['users'] as $user) {
			$template->assign_block_vars('databaserow.userrow', array(
				'NAME' => $user['name'],
				'PRIVS' => $user['privs'],
				'U_DELETE' => 'postgresql_user_unlink.php?db='.$row['database_name'].'&user='.$user['name'],
				)
			);
		}
	$i++;
	}	
}

//
// THAT'S ALL FOLKS!
// For the list of databases and active users, anyhow.
//

//
// Global page variables
//
$template->assign_vars(array(
	'S_CHOOSE_DATABASE' => '<select name=db><cpanel Postgres="listdbsopt()"></select>',
	'S_CHOOSE_USER' => '<select name=user><cpanel Postgres="listusersopt()"></select>',
	'S_ADD_USER_TO_DB_ACTION' => 'postgresql_user_link_done.php',
	
	'S_ADD_DB_ACTION' => 'postgresql_db_add_done.php',
	
	'S_ADD_USER_ACTION' => 'postgresql_user_add_done.php',
	
	'S_ADD_ACCESS_HOST_ACTION' => 'postgresql_host_add_done.php',
	
	'U_PHPPGADMIN' => '/3rdparty/phpPgAdmin/',
	
	'L_POSTGRESQL' => $lang['postgresql'],
	'L_POSTGRESQL_INTRO' => $lang['postgresql_intro'],
	'L_PHPPGADMIN_INTRO' => $lang['phpmyadmin_intro'],
	'L_DATABASE_NAME' => $lang['database_name'],
	'L_DATABASE_ACTIONS' => $lang['database_actions'],
	'L_DATABASE_USERS' => $lang['database_users'],
	
	'L_DELETE' => $lang['delete'],
	'L_REMOVE' => $lang['remove'],
	
	'L_LINK_USER_DATABASE' => $lang['link_user_database'],
	'L_LINK_USER_DATABASE_INTRO' => $lang['link_user_database_intro'],
	
	'L_USER' => $lang['user'],
	'L_DATABASE' => $lang['database'],
	'L_PRIVILEGES' => $lang['privileges'],
	
	'L_ALL' => $lang['all'],
	'L_ALTER' => $lang['alter'],
	'L_CREATE_TEMPORARY_TABLES' => $lang['create_temporary_tables'],
	'L_CREATE' => $lang['create'],
	'L_DROP' => $lang['drop'],
	'L_SELECT' => $lang['select'],
	'L_INSERT' => $lang['insert'],
	'L_UPDATE' => $lang['update'],
	'L_REFERENCES' => $lang['references'],
	'L_INDEX' => $lang['index'],
	'L_LOCK_TABLES' => $lang['lock_tables'],
	
	'L_CREATE_DATABASE' => $lang['create_database'],
	
	'L_USERS' => $lang['users'],
	'L_DATABASE_USERS_INTRO' => $lang['database_users_intro'],
	'L_USERNAME' => $lang['username'],
	'L_ACTIONS' => $lang['actions'],
	'L_PASSWORD' => $lang['password'],
	'L_ADD_USER' => $lang['add_user'],
	
	'L_ACCESS_HOSTS' => $lang['access_hosts'],
	'L_ACCESS_HOSTS_INTRO' => $lang['access_hosts_intro'],
	'L_ADDRESS' => $lang['address'],
	'L_HOST' => $lang['host'],
	'L_ADD_HOST' => $lang['add_host'],
	)
);

//
// Parse the listing of currently available users
//


$users_with_junk = <<<EOF
<cpanel Postgres="listusers()">
EOF;

$users_in_array_with_junk = explode('<br>', $users_with_junk);

$i=0;
$users_in_array = array();

foreach($users_in_array_with_junk as $row) {
	
	$temp = explode(' ', $row);
	$users_in_array[$i] = $temp[0];
	$i++;

}

$i = 0;
foreach ($users_in_array as $row) {

	if (!empty($row)) {
		$template->assign_block_vars('listuserrow', array(
			'ROWCLASS' => ($i%2) ? $template_cfg['class_row_1'] : $template_cfg['class_row_2'],
			'NAME' => $row,
			'U_DELETE' => 'postgresql_user_delete.php?user='.$row,
			)
		);
	$i++;
	}	
}



//
// Woohoo! All done!
//
$template->pparse('body');




include $root_path.'page_tail.php';




?>